<template>
  <div id="userLoginPage">
    <div class="login-container">

      <div class="info-section">
        <div class="info-content">
          <div class="logo-section">
            <img src="/src/assets/logo.png" alt="Logo" class="logo">
            <h1 class="brand-name">AI应用生成</h1>
          </div>
          <h2 class="welcome-title">欢迎回来</h2>
          <p class="welcome-subtitle">登录您的账户，开始创建智能应用</p>
          <div class="features">
            <div class="feature-item">
              <CheckCircleTwoTone /><span>零代码应用生成</span>
            </div>
            <div class="feature-item">
              <CheckCircleTwoTone /><span>AI智能助手</span>
            </div>
            <div class="feature-item">
              <CheckCircleTwoTone /><span>快速部署上线</span>
            </div>
          </div>
        </div>
      </div>

      <div class="form-section">
        <h2 class="title">用户登录</h2>
        <div class="desc">不写一行代码，生成完整应用</div>
        <a-form :model="formState" name="basic" autocomplete="off" @finish="handleSubmit">
          <a-form-item name="userAccount" :rules="[{ required: true, message: '请输入账号' }]">
            <a-input v-model:value="formState.userAccount" placeholder="请输入账号" size="large" />
          </a-form-item>
          <a-form-item name="userPassword" :rules="[
            { required: true, message: '请输入密码' },
            { min: 8, message: '密码长度不能小于 8 位' },
          ]">
            <a-input-password v-model:value="formState.userPassword" placeholder="请输入密码" size="large" />
          </a-form-item>
          <div class="tips">
            没有账号
            <RouterLink to="/user/register">去注册</RouterLink>
          </div>
          <a-form-item>
            <a-button type="primary" html-type="submit" style="width: 100%" size="large">登 录</a-button>
          </a-form-item>
        </a-form>
      </div>

    </div>
  </div>
</template>

<script lang="ts" setup>
import { CheckCircleTwoTone } from '@ant-design/icons-vue';
import { reactive } from 'vue'
import { userLogin } from '@/api/userController.ts'
import { useLoginUserStore } from '@/stores/loginUser.ts'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'

const formState = reactive<API.UserLoginRequest>({
  userAccount: '',
  userPassword: '',
})

const router = useRouter()
const loginUserStore = useLoginUserStore()

/**
 * 提交表单
 * @param values
 */
const handleSubmit = async (values: any) => {
  const res = await userLogin(values)
  // 登录成功，把登录态保存到全局状态中
  if (res.data.code === 0 && res.data.data) {
    await loginUserStore.fetchLoginUser()
    message.success('登录成功')
    router.push({
      path: '/',
      replace: true,
    })
  } else {
    message.error('登录失败，' + res.data.message)
  }
}
</script>

<style scoped>
#userLoginPage {
  background: #f5f5f5;
  width: 100%;
  height: calc(100vh - 64px);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
}

.login-container {
  max-width: 900px;
  width: 100%;
  min-height: 460px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  display: flex;
}

.info-section {
  width: 50%;
  background-color: #fafafa;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 48px;
}

.info-content {
  text-align: center;
}

.logo-section {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 32px;
}

.logo {
  width: 48px;
  height: 48px;
  border-radius: 12px;
}

.brand-name {
  font-size: 24px;
  font-weight: 600;
  margin: 0;
  color: #262626;
}

.welcome-title {
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 18px;
  color: #262626;
}

.welcome-subtitle {
  font-size: 15px;
  margin: 0 0 30px;
  color: #8c8c8c;
}

.features {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 14px;
  color: #595959;
}

.feature-icon {
  color: #1890ff;
  font-size: 16px;
}

.form-section {
  width: 50%;
  padding: 48px;
  background: #fff;
}

.title {
  text-align: center;
  margin-bottom: 16px;
}

.desc {
  text-align: center;
  color: #bbb;
  margin-bottom: 16px;
}

.tips {
  text-align: right;
  color: #bbb;
  font-size: 13px;
  margin-bottom: 16px;
}
</style>
